package com.test.mylotterysystem.service.Impl;

import com.test.mylotterysystem.comment.errorCode.ServiceErrorCode;
import com.test.mylotterysystem.comment.exception.ServiceException;
import com.test.mylotterysystem.comment.utils.JacksonUtil;
import com.test.mylotterysystem.constants.Constants;
import com.test.mylotterysystem.controller.Param.DrawPrizeParam;
import com.test.mylotterysystem.dao.DO.ActivityDO;
import com.test.mylotterysystem.dao.DO.PrizeDO;
import com.test.mylotterysystem.dao.DO.UserDO;
import com.test.mylotterysystem.dao.DO.WinnerDO;
import com.test.mylotterysystem.dao.Mapper.ActivityMapper;
import com.test.mylotterysystem.dao.Mapper.PrizeMapper;
import com.test.mylotterysystem.dao.Mapper.UserMapper;
import com.test.mylotterysystem.dao.Mapper.WinnerMapper;
import com.test.mylotterysystem.service.DrawPrizeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DrawPrizeServiceImpl implements DrawPrizeService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private PrizeMapper prizeMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private WinnerMapper winnerMapper;

    /**
     * 将前端传递过来的数据存储到rabbitMQ中
     *
     * @param drawPrizeParam
     * @return
     */
    @Override
    public void drawPrize(DrawPrizeParam drawPrizeParam) {
        if (drawPrizeParam == null) {
            throw new ServiceException(ServiceErrorCode.DRAW_PRIZE_PARAM_IS_EMPTY);
        }

        Map<String, String> map = new HashMap<>();
        map.put("messageId", UUID.randomUUID().toString().replace("-", ""));
        map.put("message", JacksonUtil.writeValueAsString(drawPrizeParam));
        rabbitTemplate.convertAndSend(Constants.EXCHANGE_NAME, Constants.KEY, map);

        log.info("消息传递成功");
    }

    /**
     * 存储获奖人员数据
     * @param message
     * @return
     */
    @Override
    public List<WinnerDO> saveWinner(DrawPrizeParam message) {
        ActivityDO activityDO = activityMapper.selectActivityById(message.getActivityId());
        PrizeDO prizeDO = prizeMapper.getPrizeById(message.getPrizeId());
        List<DrawPrizeParam.Winner> winnerList = message.getWinnerList();

        List<Long> userId = winnerList.stream().map(winner -> winner.getUserId()).collect(Collectors.toList());
        List<UserDO> userDOS = userMapper.selectUserInfoByIds(userId);

        List<WinnerDO> winnerDOS = winnerList.stream().map(winner -> {
            WinnerDO winnerDO = new WinnerDO();
            winnerDO.setActivityId(message.getActivityId());
            winnerDO.setActivityName(activityDO.getActivityName());
            winnerDO.setPrizeId(message.getPrizeId());
            winnerDO.setPrizeName(prizeDO.getName());
            winnerDO.setPrizeTier(message.getPrizeTiers());
            winnerDO.setWinnerId(winner.getUserId());
            winnerDO.setWinnerName(winner.getUserName());
            winnerDO.setWinningTime(message.getWinningTime());


            Optional<UserDO> first = userDOS.stream().filter(userDO -> winner.getUserId() == userDO.getId()).findFirst();

            first.ifPresent(temp->{
                winnerDO.setWinnerEmail(temp.getEmail());
                winnerDO.setWinnerPhoneNumber(temp.getPhoneNumber());
            });

            return winnerDO;
        }).collect(Collectors.toList());

        winnerMapper.insertWinner(winnerDOS);

        return winnerDOS;
    }
}
